package com.ta.scheduled;

import com.ta.mapper.ProductMapper;
import com.ta.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ScheduledModify{
    @Autowired
    ProductMapper productMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Value("${product.modifyWeight}")
    private String modifyProductWeight;

    /*每晚更新商品权重*/
    @Scheduled(cron = "* * 0 * * ? ")
    public void modifyPriority(){
        List<Product> list = redisTemplate.opsForHash().values(modifyProductWeight);
        for (Product product : list) {
            Product pro = new Product();
            pro.setPriority(product.getPriority());
            pro.setId(product.getId());
            /*持久化到mysql*/
            Integer modify = productMapper.modify(pro);
            /*删除保存的hash集合*/
            redisTemplate.opsForHash().delete(modifyProductWeight,product.getId().toString());
        }
    }
}
